home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / info / lispref.info-9 < prev    next >
Encoding:
GNU Info File  |  1995-09-01  |  50.8 KB  |  1,270 lines

  1. This is Info file ../../info/lispref.info, produced by Makeinfo-1.63
  2. from the input file lispref.texi.
  3.  
  4.    Edition History:
  5.  
  6.    GNU Emacs Lisp Reference Manual Second Edition (v2.01), May 1993 GNU
  7. Emacs Lisp Reference Manual Further Revised (v2.02), August 1993 Lucid
  8. Emacs Lisp Reference Manual (for 19.10) First Edition, March 1994
  9. XEmacs Lisp Programmer's Manual (for 19.12) Second Edition, April 1995
  10. GNU Emacs Lisp Reference Manual v2.4, June 1995 XEmacs Lisp
  11. Programmer's Manual (for 19.13) Third Edition, July 1995
  12.  
  13.    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995 Free Software
  14. Foundation, Inc.  Copyright (C) 1994, 1995 Sun Microsystems, Inc.
  15. Copyright (C) 1995 Amdahl Corporation.  Copyright (C) 1995 Ben Wing.
  16.  
  17.    Permission is granted to make and distribute verbatim copies of this
  18. manual provided the copyright notice and this permission notice are
  19. preserved on all copies.
  20.  
  21.    Permission is granted to copy and distribute modified versions of
  22. this manual under the conditions for verbatim copying, provided that the
  23. entire resulting derived work is distributed under the terms of a
  24. permission notice identical to this one.
  25.  
  26.    Permission is granted to copy and distribute translations of this
  27. manual into another language, under the above conditions for modified
  28. versions, except that this permission notice may be stated in a
  29. translation approved by the Foundation.
  30.  
  31.    Permission is granted to copy and distribute modified versions of
  32. this manual under the conditions for verbatim copying, provided also
  33. that the section entitled "GNU General Public License" is included
  34. exactly as in the original, and provided that the entire resulting
  35. derived work is distributed under the terms of a permission notice
  36. identical to this one.
  37.  
  38.    Permission is granted to copy and distribute translations of this
  39. manual into another language, under the above conditions for modified
  40. versions, except that the section entitled "GNU General Public License"
  41. may be included in a translation approved by the Free Software
  42. Foundation instead of in the original English.
  43.  
  44. 
  45. File: lispref.info,  Node: Intro to Buffer-Local,  Next: Creating Buffer-Local,  Up: Buffer-Local Variables
  46.  
  47. Introduction to Buffer-Local Variables
  48. --------------------------------------
  49.  
  50.    A buffer-local variable has a buffer-local binding associated with a
  51. particular buffer.  The binding is in effect when that buffer is
  52. current; otherwise, it is not in effect.  If you set the variable while
  53. a buffer-local binding is in effect, the new value goes in that binding,
  54. so the global binding is unchanged; this means that the change is
  55. visible in that buffer alone.
  56.  
  57.    A variable may have buffer-local bindings in some buffers but not in
  58. others.  The global binding is shared by all the buffers that don't have
  59. their own bindings.  Thus, if you set the variable in a buffer that does
  60. not have a buffer-local binding for it, the new value is visible in all
  61. buffers except those with buffer-local bindings.  (Here we are assuming
  62. that there are no `let'-style local bindings to complicate the issue.)
  63.  
  64.    The most common use of buffer-local bindings is for major modes to
  65. change variables that control the behavior of commands.  For example, C
  66. mode and Lisp mode both set the variable `paragraph-start' to specify
  67. that only blank lines separate paragraphs.  They do this by making the
  68. variable buffer-local in the buffer that is being put into C mode or
  69. Lisp mode, and then setting it to the new value for that mode.
  70.  
  71.    The usual way to make a buffer-local binding is with
  72. `make-local-variable', which is what major mode commands use.  This
  73. affects just the current buffer; all other buffers (including those yet
  74. to be created) continue to share the global value.
  75.  
  76.    A more powerful operation is to mark the variable as "automatically
  77. buffer-local" by calling `make-variable-buffer-local'.  You can think
  78. of this as making the variable local in all buffers, even those yet to
  79. be created.  More precisely, the effect is that setting the variable
  80. automatically makes the variable local to the current buffer if it is
  81. not already so.  All buffers start out by sharing the global value of
  82. the variable as usual, but any `setq' creates a buffer-local binding
  83. for the current buffer.  The new value is stored in the buffer-local
  84. binding, leaving the (default) global binding untouched.  The global
  85. value can no longer be changed with `setq'; you need to use
  86. `setq-default' to do that.
  87.  
  88.    Local variables in a file you edit are also represented by
  89. buffer-local bindings for the buffer that holds the file within XEmacs.
  90. *Note Auto Major Mode::.
  91.  
  92. 
  93. File: lispref.info,  Node: Creating Buffer-Local,  Next: Default Value,  Prev: Intro to Buffer-Local,  Up: Buffer-Local Variables
  94.  
  95. Creating and Deleting Buffer-Local Bindings
  96. -------------------------------------------
  97.  
  98.  - Command: make-local-variable VARIABLE
  99.      This function creates a buffer-local binding in the current buffer
  100.      for VARIABLE (a symbol).  Other buffers are not affected.  The
  101.      value returned is VARIABLE.
  102.  
  103.      The buffer-local value of VARIABLE starts out as the same value
  104.      VARIABLE previously had.  If VARIABLE was void, it remains void.
  105.  
  106.           ;; In buffer `b1':
  107.           (setq foo 5)                ; Affects all buffers.
  108.                => 5
  109.           (make-local-variable 'foo)  ; Now it is local in `b1'.
  110.                => foo
  111.           foo                         ; That did not change
  112.                => 5                   ;   the value.
  113.           (setq foo 6)                ; Change the value
  114.                => 6                   ;   in `b1'.
  115.           foo
  116.                => 6
  117.           
  118.           ;; In buffer `b2', the value hasn't changed.
  119.           (save-excursion
  120.             (set-buffer "b2")
  121.             foo)
  122.                => 5
  123.  
  124.      Making a variable buffer-local within a `let'-binding for that
  125.      variable does not work.  This is because `let' does not distinguish
  126.      between different kinds of bindings; it knows only which variable
  127.      the binding was made for.
  128.  
  129.      *Note:* do not use `make-local-variable' for a hook variable.
  130.      Instead, use `make-local-hook'.  *Note Hooks::.
  131.  
  132.  - Command: make-variable-buffer-local VARIABLE
  133.      This function marks VARIABLE (a symbol) automatically
  134.      buffer-local, so that any subsequent attempt to set it will make it
  135.      local to the current buffer at the time.
  136.  
  137.      The value returned is VARIABLE.
  138.  
  139.  - Function: local-variable-p VARIABLE &optional BUFFER
  140.      This returns `t' if VARIABLE is buffer-local in buffer BUFFER
  141.      (which defaults to the current buffer); otherwise, `nil'.
  142.  
  143.  - Function: buffer-local-variables &optional BUFFER
  144.      This function returns a list describing the buffer-local variables
  145.      in buffer BUFFER.  It returns an association list (*note
  146.      Association Lists::.) in which each association contains one
  147.      buffer-local variable and its value.  When a buffer-local variable
  148.      is void in BUFFER, then it appears directly in the resulting list.
  149.      If BUFFER is omitted, the current buffer is used.
  150.  
  151.           (make-local-variable 'foobar)
  152.           (makunbound 'foobar)
  153.           (make-local-variable 'bind-me)
  154.           (setq bind-me 69)
  155.           (setq lcl (buffer-local-variables))
  156.               ;; First, built-in variables local in all buffers:
  157.           => ((mark-active . nil)
  158.               (buffer-undo-list nil)
  159.               (mode-name . "Fundamental")
  160.               ...
  161.               ;; Next, non-built-in local variables.
  162.               ;; This one is local and void:
  163.               foobar
  164.               ;; This one is local and nonvoid:
  165.               (bind-me . 69))
  166.  
  167.      Note that storing new values into the CDRs of cons cells in this
  168.      list does *not* change the local values of the variables.
  169.  
  170.  - Command: kill-local-variable VARIABLE
  171.      This function deletes the buffer-local binding (if any) for
  172.      VARIABLE (a symbol) in the current buffer.  As a result, the
  173.      global (default) binding of VARIABLE becomes visible in this
  174.      buffer.  Usually this results in a change in the value of
  175.      VARIABLE, since the global value is usually different from the
  176.      buffer-local value just eliminated.
  177.  
  178.      If you kill the local binding of a variable that automatically
  179.      becomes local when set, this makes the global value visible in the
  180.      current buffer.  However, if you set the variable again, that will
  181.      once again create a local binding for it.
  182.  
  183.      `kill-local-variable' returns VARIABLE.
  184.  
  185.      This function is a command because it is sometimes useful to kill
  186.      one buffer-local variable interactively, just as it is useful to
  187.      create buffer-local variables interactively.
  188.  
  189.  - Function: kill-all-local-variables
  190.      This function eliminates all the buffer-local variable bindings of
  191.      the current buffer except for variables marked as "permanent".  As
  192.      a result, the buffer will see the default values of most variables.
  193.  
  194.      This function also resets certain other information pertaining to
  195.      the buffer: it sets the local keymap to `nil', the syntax table to
  196.      the value of `standard-syntax-table', and the abbrev table to the
  197.      value of `fundamental-mode-abbrev-table'.
  198.  
  199.      Every major mode command begins by calling this function, which
  200.      has the effect of switching to Fundamental mode and erasing most
  201.      of the effects of the previous major mode.  To ensure that this
  202.      does its job, the variables that major modes set should not be
  203.      marked permanent.
  204.  
  205.      `kill-all-local-variables' returns `nil'.
  206.  
  207.    A local variable is "permanent" if the variable name (a symbol) has a
  208. `permanent-local' property that is non-`nil'.  Permanent locals are
  209. appropriate for data pertaining to where the file came from or how to
  210. save it, rather than with how to edit the contents.
  211.  
  212. 
  213. File: lispref.info,  Node: Default Value,  Prev: Creating Buffer-Local,  Up: Buffer-Local Variables
  214.  
  215. The Default Value of a Buffer-Local Variable
  216. --------------------------------------------
  217.  
  218.    The global value of a variable with buffer-local bindings is also
  219. called the "default" value, because it is the value that is in effect
  220. except when specifically overridden.
  221.  
  222.    The functions `default-value' and `setq-default' access and change a
  223. variable's default value regardless of whether the current buffer has a
  224. buffer-local binding.  For example, you could use `setq-default' to
  225. change the default setting of `paragraph-start' for most buffers; and
  226. this would work even when you are in a C or Lisp mode buffer that has a
  227. buffer-local value for this variable.
  228.  
  229.    The special forms `defvar' and `defconst' also set the default value
  230. (if they set the variable at all), rather than any local value.
  231.  
  232.  - Function: default-value SYMBOL
  233.      This function returns SYMBOL's default value.  This is the value
  234.      that is seen in buffers that do not have their own values for this
  235.      variable.  If SYMBOL is not buffer-local, this is equivalent to
  236.      `symbol-value' (*note Accessing Variables::.).
  237.  
  238.  - Function: default-boundp SYMBOL
  239.      The function `default-boundp' tells you whether SYMBOL's default
  240.      value is nonvoid.  If `(default-boundp 'foo)' returns `nil', then
  241.      `(default-value 'foo)' would get an error.
  242.  
  243.      `default-boundp' is to `default-value' as `boundp' is to
  244.      `symbol-value'.
  245.  
  246.  - Special Form: setq-default SYMBOL VALUE
  247.      This sets the default value of SYMBOL to VALUE.  It does not
  248.      evaluate SYMBOL, but does evaluate VALUE.  The value of the
  249.      `setq-default' form is VALUE.
  250.  
  251.      If a SYMBOL is not buffer-local for the current buffer, and is not
  252.      marked automatically buffer-local, `setq-default' has the same
  253.      effect as `setq'.  If SYMBOL is buffer-local for the current
  254.      buffer, then this changes the value that other buffers will see
  255.      (as long as they don't have a buffer-local value), but not the
  256.      value that the current buffer sees.
  257.  
  258.           ;; In buffer `foo':
  259.           (make-local-variable 'local)
  260.                => local
  261.           (setq local 'value-in-foo)
  262.                => value-in-foo
  263.           (setq-default local 'new-default)
  264.                => new-default
  265.           local
  266.                => value-in-foo
  267.           (default-value 'local)
  268.                => new-default
  269.           
  270.           ;; In (the new) buffer `bar':
  271.           local
  272.                => new-default
  273.           (default-value 'local)
  274.                => new-default
  275.           (setq local 'another-default)
  276.                => another-default
  277.           (default-value 'local)
  278.                => another-default
  279.           
  280.           ;; Back in buffer `foo':
  281.           local
  282.                => value-in-foo
  283.           (default-value 'local)
  284.                => another-default
  285.  
  286.  - Function: set-default SYMBOL VALUE
  287.      This function is like `setq-default', except that SYMBOL is
  288.      evaluated.
  289.  
  290.           (set-default (car '(a b c)) 23)
  291.                => 23
  292.           (default-value 'a)
  293.                => 23
  294.  
  295. 
  296. File: lispref.info,  Node: Functions,  Next: Macros,  Prev: Variables,  Up: Top
  297.  
  298. Functions
  299. *********
  300.  
  301.    A Lisp program is composed mainly of Lisp functions.  This chapter
  302. explains what functions are, how they accept arguments, and how to
  303. define them.
  304.  
  305. * Menu:
  306.  
  307. * What Is a Function::    Lisp functions vs. primitives; terminology.
  308. * Lambda Expressions::    How functions are expressed as Lisp objects.
  309. * Function Names::        A symbol can serve as the name of a function.
  310. * Defining Functions::    Lisp expressions for defining functions.
  311. * Calling Functions::     How to use an existing function.
  312. * Mapping Functions::     Applying a function to each element of a list, etc.
  313. * Anonymous Functions::   Lambda expressions are functions with no names.
  314. * Function Cells::        Accessing or setting the function definition
  315.                             of a symbol.
  316. * Inline Functions::      Defining functions that the compiler will open code.
  317. * Related Topics::        Cross-references to specific Lisp primitives
  318.                             that have a special bearing on how functions work.
  319.  
  320. 
  321. File: lispref.info,  Node: What Is a Function,  Next: Lambda Expressions,  Up: Functions
  322.  
  323. What Is a Function?
  324. ===================
  325.  
  326.    In a general sense, a function is a rule for carrying on a
  327. computation given several values called "arguments".  The result of the
  328. computation is called the value of the function.  The computation can
  329. also have side effects: lasting changes in the values of variables or
  330. the contents of data structures.
  331.  
  332.    Here are important terms for functions in Emacs Lisp and for other
  333. function-like objects.
  334.  
  335. "function"
  336.      In Emacs Lisp, a "function" is anything that can be applied to
  337.      arguments in a Lisp program.  In some cases, we use it more
  338.      specifically to mean a function written in Lisp.  Special forms and
  339.      macros are not functions.
  340.  
  341. "primitive"
  342.      A "primitive" is a function callable from Lisp that is written in
  343.      C, such as `car' or `append'.  These functions are also called
  344.      "built-in" functions or "subrs".  (Special forms are also
  345.      considered primitives.)
  346.  
  347.      Usually the reason that a function is a primitives is because it is
  348.      fundamental, because it provides a low-level interface to operating
  349.      system services, or because it needs to run fast.  Primitives can
  350.      be modified or added only by changing the C sources and
  351.      recompiling the editor.  See *Note Writing XEmacs Primitives::.
  352.  
  353. "lambda expression"
  354.      A "lambda expression" is a function written in Lisp.  These are
  355.      described in the following section.
  356.  
  357.      *Note Lambda Expressions::.
  358.  
  359. "special form"
  360.      A "special form" is a primitive that is like a function but does
  361.      not evaluate all of its arguments in the usual way.  It may
  362.      evaluate only some of the arguments, or may evaluate them in an
  363.      unusual order, or several times.  Many special forms are described
  364.      in *Note Control Structures::.
  365.  
  366. "macro"
  367.      A "macro" is a construct defined in Lisp by the programmer.  It
  368.      differs from a function in that it translates a Lisp expression
  369.      that you write into an equivalent expression to be evaluated
  370.      instead of the original expression.  Macros enable Lisp
  371.      programmers to do the sorts of things that special forms can do.
  372.      *Note Macros::, for how to define and use macros.
  373.  
  374. "command"
  375.      A "command" is an object that `command-execute' can invoke; it is
  376.      a possible definition for a key sequence.  Some functions are
  377.      commands; a function written in Lisp is a command if it contains an
  378.      interactive declaration (*note Defining Commands::.).  Such a
  379.      function can be called from Lisp expressions like other functions;
  380.      in this case, the fact that the function is a command makes no
  381.      difference.
  382.  
  383.      Keyboard macros (strings and vectors) are commands also, even
  384.      though they are not functions.  A symbol is a command if its
  385.      function definition is a command; such symbols can be invoked with
  386.      `M-x'.  The symbol is a function as well if the definition is a
  387.      function.  *Note Command Overview::.
  388.  
  389. "keystroke command"
  390.      A "keystroke command" is a command that is bound to a key sequence
  391.      (typically one to three keystrokes).  The distinction is made here
  392.      merely to avoid confusion with the meaning of "command" in
  393.      non-Emacs editors; for Lisp programs, the distinction is normally
  394.      unimportant.
  395.  
  396. "byte-code function"
  397.      A "byte-code function" is a function that has been compiled by the
  398.      byte compiler.  *Note Byte-Code Type::.
  399.  
  400.  - Function: subrp OBJECT
  401.      This function returns `t' if OBJECT is a built-in function (i.e.,
  402.      a Lisp primitive).
  403.  
  404.           (subrp 'message)            ; `message' is a symbol,
  405.                => nil                 ;   not a subr object.
  406.           (subrp (symbol-function 'message))
  407.                => t
  408.  
  409.  - Function: byte-code-function-p OBJECT
  410.      This function returns `t' if OBJECT is a byte-code function.  For
  411.      example:
  412.  
  413.           (byte-code-function-p (symbol-function 'next-line))
  414.                => t
  415.  
  416. 
  417. File: lispref.info,  Node: Lambda Expressions,  Next: Function Names,  Prev: What Is a Function,  Up: Functions
  418.  
  419. Lambda Expressions
  420. ==================
  421.  
  422.    A function written in Lisp is a list that looks like this:
  423.  
  424.      (lambda (ARG-VARIABLES...)
  425.        [DOCUMENTATION-STRING]
  426.        [INTERACTIVE-DECLARATION]
  427.        BODY-FORMS...)
  428.  
  429. Such a list is called a "lambda expression".  In Emacs Lisp, it
  430. actually is valid as an expression--it evaluates to itself.  In some
  431. other Lisp dialects, a lambda expression is not a valid expression at
  432. all.  In either case, its main use is not to be evaluated as an
  433. expression, but to be called as a function.
  434.  
  435. * Menu:
  436.  
  437. * Lambda Components::       The parts of a lambda expression.
  438. * Simple Lambda::           A simple example.
  439. * Argument List::           Details and special features of argument lists.
  440. * Function Documentation::  How to put documentation in a function.
  441.  
  442. 
  443. File: lispref.info,  Node: Lambda Components,  Next: Simple Lambda,  Up: Lambda Expressions
  444.  
  445. Components of a Lambda Expression
  446. ---------------------------------
  447.  
  448.    A function written in Lisp (a "lambda expression") is a list that
  449. looks like this:
  450.  
  451.      (lambda (ARG-VARIABLES...)
  452.        [DOCUMENTATION-STRING]
  453.        [INTERACTIVE-DECLARATION]
  454.        BODY-FORMS...)
  455.  
  456.    The first element of a lambda expression is always the symbol
  457. `lambda'.  This indicates that the list represents a function.  The
  458. reason functions are defined to start with `lambda' is so that other
  459. lists, intended for other uses, will not accidentally be valid as
  460. functions.
  461.  
  462.    The second element is a list of symbols-the argument variable names.
  463. This is called the "lambda list".  When a Lisp function is called, the
  464. argument values are matched up against the variables in the lambda
  465. list, which are given local bindings with the values provided.  *Note
  466. Local Variables::.
  467.  
  468.    The documentation string is a Lisp string object placed within the
  469. function definition to describe the function for the XEmacs help
  470. facilities.  *Note Function Documentation::.
  471.  
  472.    The interactive declaration is a list of the form `(interactive
  473. CODE-STRING)'.  This declares how to provide arguments if the function
  474. is used interactively.  Functions with this declaration are called
  475. "commands"; they can be called using `M-x' or bound to a key.
  476. Functions not intended to be called in this way should not have
  477. interactive declarations.  *Note Defining Commands::, for how to write
  478. an interactive declaration.
  479.  
  480.    The rest of the elements are the "body" of the function: the Lisp
  481. code to do the work of the function (or, as a Lisp programmer would say,
  482. "a list of Lisp forms to evaluate").  The value returned by the
  483. function is the value returned by the last element of the body.
  484.  
  485. 
  486. File: lispref.info,  Node: Simple Lambda,  Next: Argument List,  Prev: Lambda Components,  Up: Lambda Expressions
  487.  
  488. A Simple Lambda-Expression Example
  489. ----------------------------------
  490.  
  491.    Consider for example the following function:
  492.  
  493.      (lambda (a b c) (+ a b c))
  494.  
  495. We can call this function by writing it as the CAR of an expression,
  496. like this:
  497.  
  498.      ((lambda (a b c) (+ a b c))
  499.       1 2 3)
  500.  
  501. This call evaluates the body of the lambda expression  with the variable
  502. `a' bound to 1, `b' bound to 2, and `c' bound to 3.  Evaluation of the
  503. body adds these three numbers, producing the result 6; therefore, this
  504. call to the function returns the value 6.
  505.  
  506.    Note that the arguments can be the results of other function calls,
  507. as in this example:
  508.  
  509.      ((lambda (a b c) (+ a b c))
  510.       1 (* 2 3) (- 5 4))
  511.  
  512. This evaluates the arguments `1', `(* 2 3)', and `(- 5 4)' from left to
  513. right.  Then it applies the lambda expression to the argument values 1,
  514. 6 and 1 to produce the value 8.
  515.  
  516.    It is not often useful to write a lambda expression as the CAR of a
  517. form in this way.  You can get the same result, of making local
  518. variables and giving them values, using the special form `let' (*note
  519. Local Variables::.).  And `let' is clearer and easier to use.  In
  520. practice, lambda expressions are either stored as the function
  521. definitions of symbols, to produce named functions, or passed as
  522. arguments to other functions (*note Anonymous Functions::.).
  523.  
  524.    However, calls to explicit lambda expressions were very useful in the
  525. old days of Lisp, before the special form `let' was invented.  At that
  526. time, they were the only way to bind and initialize local variables.
  527.  
  528. 
  529. File: lispref.info,  Node: Argument List,  Next: Function Documentation,  Prev: Simple Lambda,  Up: Lambda Expressions
  530.  
  531. Advanced Features of Argument Lists
  532. -----------------------------------
  533.  
  534.    Our simple sample function, `(lambda (a b c) (+ a b c))', specifies
  535. three argument variables, so it must be called with three arguments: if
  536. you try to call it with only two arguments or four arguments, you get a
  537. `wrong-number-of-arguments' error.
  538.  
  539.    It is often convenient to write a function that allows certain
  540. arguments to be omitted.  For example, the function `substring' accepts
  541. three arguments--a string, the start index and the end index--but the
  542. third argument defaults to the LENGTH of the string if you omit it.  It
  543. is also convenient for certain functions to accept an indefinite number
  544. of arguments, as the functions `list' and `+' do.
  545.  
  546.    To specify optional arguments that may be omitted when a function is
  547. called, simply include the keyword `&optional' before the optional
  548. arguments.  To specify a list of zero or more extra arguments, include
  549. the keyword `&rest' before one final argument.
  550.  
  551.    Thus, the complete syntax for an argument list is as follows:
  552.  
  553.      (REQUIRED-VARS...
  554.       [&optional OPTIONAL-VARS...]
  555.       [&rest REST-VAR])
  556.  
  557. The square brackets indicate that the `&optional' and `&rest' clauses,
  558. and the variables that follow them, are optional.
  559.  
  560.    A call to the function requires one actual argument for each of the
  561. REQUIRED-VARS.  There may be actual arguments for zero or more of the
  562. OPTIONAL-VARS, and there cannot be any actual arguments beyond that
  563. unless the lambda list uses `&rest'.  In that case, there may be any
  564. number of extra actual arguments.
  565.  
  566.    If actual arguments for the optional and rest variables are omitted,
  567. then they always default to `nil'.  There is no way for the function to
  568. distinguish between an explicit argument of `nil' and an omitted
  569. argument.  However, the body of the function is free to consider `nil'
  570. an abbreviation for some other meaningful value.  This is what
  571. `substring' does; `nil' as the third argument to `substring' means to
  572. use the length of the string supplied.
  573.  
  574.      Common Lisp note: Common Lisp allows the function to specify what
  575.      default value to use when an optional argument is omitted; Emacs
  576.      Lisp always uses `nil'.
  577.  
  578.    For example, an argument list that looks like this:
  579.  
  580.      (a b &optional c d &rest e)
  581.  
  582. binds `a' and `b' to the first two actual arguments, which are
  583. required.  If one or two more arguments are provided, `c' and `d' are
  584. bound to them respectively; any arguments after the first four are
  585. collected into a list and `e' is bound to that list.  If there are only
  586. two arguments, `c' is `nil'; if two or three arguments, `d' is `nil';
  587. if four arguments or fewer, `e' is `nil'.
  588.  
  589.    There is no way to have required arguments following optional
  590. ones--it would not make sense.  To see why this must be so, suppose
  591. that `c' in the example were optional and `d' were required.  Suppose
  592. three actual arguments are given; which variable would the third
  593. argument be for?  Similarly, it makes no sense to have any more
  594. arguments (either required or optional) after a `&rest' argument.
  595.  
  596.    Here are some examples of argument lists and proper calls:
  597.  
  598.      ((lambda (n) (1+ n))                ; One required:
  599.       1)                                 ; requires exactly one argument.
  600.           => 2
  601.      ((lambda (n &optional n1)           ; One required and one optional:
  602.               (if n1 (+ n n1) (1+ n)))   ; 1 or 2 arguments.
  603.       1 2)
  604.           => 3
  605.      ((lambda (n &rest ns)               ; One required and one rest:
  606.               (+ n (apply '+ ns)))       ; 1 or more arguments.
  607.       1 2 3 4 5)
  608.           => 15
  609.  
  610. 
  611. File: lispref.info,  Node: Function Documentation,  Prev: Argument List,  Up: Lambda Expressions
  612.  
  613. Documentation Strings of Functions
  614. ----------------------------------
  615.  
  616.    A lambda expression may optionally have a "documentation string" just
  617. after the lambda list.  This string does not affect execution of the
  618. function; it is a kind of comment, but a systematized comment which
  619. actually appears inside the Lisp world and can be used by the XEmacs
  620. help facilities.  *Note Documentation::, for how the
  621. DOCUMENTATION-STRING is accessed.
  622.  
  623.    It is a good idea to provide documentation strings for all the
  624. functions in your program, even those that are only called from within
  625. your program.  Documentation strings are like comments, except that they
  626. are easier to access.
  627.  
  628.    The first line of the documentation string should stand on its own,
  629. because `apropos' displays just this first line.  It should consist of
  630. one or two complete sentences that summarize the function's purpose.
  631.  
  632.    The start of the documentation string is usually indented in the
  633. source file, but since these spaces come before the starting
  634. double-quote, they are not part of the string.  Some people make a
  635. practice of indenting any additional lines of the string so that the
  636. text lines up in the program source.  *This is a mistake.*  The
  637. indentation of the following lines is inside the string; what looks
  638. nice in the source code will look ugly when displayed by the help
  639. commands.
  640.  
  641.    You may wonder how the documentation string could be optional, since
  642. there are required components of the function that follow it (the body).
  643. Since evaluation of a string returns that string, without any side
  644. effects, it has no effect if it is not the last form in the body.
  645. Thus, in practice, there is no confusion between the first form of the
  646. body and the documentation string; if the only body form is a string
  647. then it serves both as the return value and as the documentation.
  648.  
  649. 
  650. File: lispref.info,  Node: Function Names,  Next: Defining Functions,  Prev: Lambda Expressions,  Up: Functions
  651.  
  652. Naming a Function
  653. =================
  654.  
  655.    In most computer languages, every function has a name; the idea of a
  656. function without a name is nonsensical.  In Lisp, a function in the
  657. strictest sense has no name.  It is simply a list whose first element is
  658. `lambda', or a primitive subr-object.
  659.  
  660.    However, a symbol can serve as the name of a function.  This happens
  661. when you put the function in the symbol's "function cell" (*note Symbol
  662. Components::.).  Then the symbol itself becomes a valid, callable
  663. function, equivalent to the list or subr-object that its function cell
  664. refers to.  The contents of the function cell are also called the
  665. symbol's "function definition".  The procedure of using a symbol's
  666. function definition in place of the symbol is called "symbol function
  667. indirection"; see *Note Function Indirection::.
  668.  
  669.    In practice, nearly all functions are given names in this way and
  670. referred to through their names.  For example, the symbol `car' works
  671. as a function and does what it does because the primitive subr-object
  672. `#<subr car>' is stored in its function cell.
  673.  
  674.    We give functions names because it is convenient to refer to them by
  675. their names in Lisp expressions.  For primitive subr-objects such as
  676. `#<subr car>', names are the only way you can refer to them: there is
  677. no read syntax for such objects.  For functions written in Lisp, the
  678. name is more convenient to use in a call than an explicit lambda
  679. expression.  Also, a function with a name can refer to itself--it can
  680. be recursive.  Writing the function's name in its own definition is much
  681. more convenient than making the function definition point to itself
  682. (something that is not impossible but that has various disadvantages in
  683. practice).
  684.  
  685.    We often identify functions with the symbols used to name them.  For
  686. example, we often speak of "the function `car'", not distinguishing
  687. between the symbol `car' and the primitive subr-object that is its
  688. function definition.  For most purposes, there is no need to
  689. distinguish.
  690.  
  691.    Even so, keep in mind that a function need not have a unique name.
  692. While a given function object *usually* appears in the function cell of
  693. only one symbol, this is just a matter of convenience.  It is easy to
  694. store it in several symbols using `fset'; then each of the symbols is
  695. equally well a name for the same function.
  696.  
  697.    A symbol used as a function name may also be used as a variable;
  698. these two uses of a symbol are independent and do not conflict.
  699.  
  700. 
  701. File: lispref.info,  Node: Defining Functions,  Next: Calling Functions,  Prev: Function Names,  Up: Functions
  702.  
  703. Defining Functions
  704. ==================
  705.  
  706.    We usually give a name to a function when it is first created.  This
  707. is called "defining a function", and it is done with the `defun'
  708. special form.
  709.  
  710.  - Special Form: defun NAME ARGUMENT-LIST BODY-FORMS
  711.      `defun' is the usual way to define new Lisp functions.  It defines
  712.      the symbol NAME as a function that looks like this:
  713.  
  714.           (lambda ARGUMENT-LIST . BODY-FORMS)
  715.  
  716.      `defun' stores this lambda expression in the function cell of
  717.      NAME.  It returns the value NAME, but usually we ignore this value.
  718.  
  719.      As described previously (*note Lambda Expressions::.),
  720.      ARGUMENT-LIST is a list of argument names and may include the
  721.      keywords `&optional' and `&rest'.  Also, the first two forms in
  722.      BODY-FORMS may be a documentation string and an interactive
  723.      declaration.
  724.  
  725.      There is no conflict if the same symbol NAME is also used as a
  726.      variable, since the symbol's value cell is independent of the
  727.      function cell.  *Note Symbol Components::.
  728.  
  729.      Here are some examples:
  730.  
  731.           (defun foo () 5)
  732.                => foo
  733.           (foo)
  734.                => 5
  735.           
  736.           (defun bar (a &optional b &rest c)
  737.               (list a b c))
  738.                => bar
  739.           (bar 1 2 3 4 5)
  740.                => (1 2 (3 4 5))
  741.           (bar 1)
  742.                => (1 nil nil)
  743.           (bar)
  744.           error--> Wrong number of arguments.
  745.           
  746.           (defun capitalize-backwards ()
  747.             "Upcase the last letter of a word."
  748.             (interactive)
  749.             (backward-word 1)
  750.             (forward-word 1)
  751.             (backward-char 1)
  752.             (capitalize-word 1))
  753.                => capitalize-backwards
  754.  
  755.      Be careful not to redefine existing functions unintentionally.
  756.      `defun' redefines even primitive functions such as `car' without
  757.      any hesitation or notification.  Redefining a function already
  758.      defined is often done deliberately, and there is no way to
  759.      distinguish deliberate redefinition from unintentional
  760.      redefinition.
  761.  
  762.  - Function: defalias NAME DEFINITION
  763.      This special form defines the symbol NAME as a function, with
  764.      definition DEFINITION (which can be any valid Lisp function).
  765.  
  766.      The proper place to use `defalias' is where a specific function
  767.      name is being defined--especially where that name appears
  768.      explicitly in the source file being loaded.  This is because
  769.      `defalias' records which file defined the function, just like
  770.      `defun' (*note Unloading::.).
  771.  
  772.      By contrast, in programs that manipulate function definitions for
  773.      other purposes, it is better to use `fset', which does not keep
  774.      such records.
  775.  
  776.    See also `defsubst', which defines a function like `defun' and tells
  777. the Lisp compiler to open-code it.  *Note Inline Functions::.
  778.  
  779. 
  780. File: lispref.info,  Node: Calling Functions,  Next: Mapping Functions,  Prev: Defining Functions,  Up: Functions
  781.  
  782. Calling Functions
  783. =================
  784.  
  785.    Defining functions is only half the battle.  Functions don't do
  786. anything until you "call" them, i.e., tell them to run.  Calling a
  787. function is also known as "invocation".
  788.  
  789.    The most common way of invoking a function is by evaluating a list.
  790. For example, evaluating the list `(concat "a" "b")' calls the function
  791. `concat' with arguments `"a"' and `"b"'.  *Note Evaluation::, for a
  792. description of evaluation.
  793.  
  794.    When you write a list as an expression in your program, the function
  795. name is part of the program.  This means that you choose which function
  796. to call, and how many arguments to give it, when you write the program.
  797. Usually that's just what you want.  Occasionally you need to decide at
  798. run time which function to call.  To do that, use the functions
  799. `funcall' and `apply'.
  800.  
  801.  - Function: funcall FUNCTION &rest ARGUMENTS
  802.      `funcall' calls FUNCTION with ARGUMENTS, and returns whatever
  803.      FUNCTION returns.
  804.  
  805.      Since `funcall' is a function, all of its arguments, including
  806.      FUNCTION, are evaluated before `funcall' is called.  This means
  807.      that you can use any expression to obtain the function to be
  808.      called.  It also means that `funcall' does not see the expressions
  809.      you write for the ARGUMENTS, only their values.  These values are
  810.      *not* evaluated a second time in the act of calling FUNCTION;
  811.      `funcall' enters the normal procedure for calling a function at the
  812.      place where the arguments have already been evaluated.
  813.  
  814.      The argument FUNCTION must be either a Lisp function or a
  815.      primitive function.  Special forms and macros are not allowed,
  816.      because they make sense only when given the "unevaluated" argument
  817.      expressions.  `funcall' cannot provide these because, as we saw
  818.      above, it never knows them in the first place.
  819.  
  820.           (setq f 'list)
  821.                => list
  822.           (funcall f 'x 'y 'z)
  823.                => (x y z)
  824.           (funcall f 'x 'y '(z))
  825.                => (x y (z))
  826.           (funcall 'and t nil)
  827.           error--> Invalid function: #<subr and>
  828.  
  829.      Compare these example with the examples of `apply'.
  830.  
  831.  - Function: apply FUNCTION &rest ARGUMENTS
  832.      `apply' calls FUNCTION with ARGUMENTS, just like `funcall' but
  833.      with one difference: the last of ARGUMENTS is a list of arguments
  834.      to give to FUNCTION, rather than a single argument.  We also say
  835.      that `apply' "spreads" this list so that each individual element
  836.      becomes an argument.
  837.  
  838.      `apply' returns the result of calling FUNCTION.  As with
  839.      `funcall', FUNCTION must either be a Lisp function or a primitive
  840.      function; special forms and macros do not make sense in `apply'.
  841.  
  842.           (setq f 'list)
  843.                => list
  844.           (apply f 'x 'y 'z)
  845.           error--> Wrong type argument: listp, z
  846.           (apply '+ 1 2 '(3 4))
  847.                => 10
  848.           (apply '+ '(1 2 3 4))
  849.                => 10
  850.           
  851.           (apply 'append '((a b c) nil (x y z) nil))
  852.                => (a b c x y z)
  853.  
  854.      For an interesting example of using `apply', see the description of
  855.      `mapcar', in *Note Mapping Functions::.
  856.  
  857.    It is common for Lisp functions to accept functions as arguments or
  858. find them in data structures (especially in hook variables and property
  859. lists) and call them using `funcall' or `apply'.  Functions that accept
  860. function arguments are often called "functionals".
  861.  
  862.    Sometimes, when you call a functional, it is useful to supply a no-op
  863. function as the argument.  Here are two different kinds of no-op
  864. function:
  865.  
  866.  - Function: identity ARG
  867.      This function returns ARG and has no side effects.
  868.  
  869.  - Function: ignore &rest ARGS
  870.      This function ignores any arguments and returns `nil'.
  871.  
  872. 
  873. File: lispref.info,  Node: Mapping Functions,  Next: Anonymous Functions,  Prev: Calling Functions,  Up: Functions
  874.  
  875. Mapping Functions
  876. =================
  877.  
  878.    A "mapping function" applies a given function to each element of a
  879. list or other collection.  Emacs Lisp has three such functions;
  880. `mapcar' and `mapconcat', which scan a list, are described here.  For
  881. the third mapping function, `mapatoms', see *Note Creating Symbols::.
  882.  
  883.  - Function: mapcar FUNCTION SEQUENCE
  884.      `mapcar' applies FUNCTION to each element of SEQUENCE in turn, and
  885.      returns a list of the results.
  886.  
  887.      The argument SEQUENCE may be a list, a vector, or a string.  The
  888.      result is always a list.  The length of the result is the same as
  889.      the length of SEQUENCE.
  890.  
  891.      For example:
  892.  
  893.           (mapcar 'car '((a b) (c d) (e f)))
  894.                => (a c e)
  895.           (mapcar '1+ [1 2 3])
  896.                => (2 3 4)
  897.           (mapcar 'char-to-string "abc")
  898.                => ("a" "b" "c")
  899.  
  900.           ;; Call each function in `my-hooks'.
  901.           (mapcar 'funcall my-hooks)
  902.  
  903.           (defun mapcar* (f &rest args)
  904.             "Apply FUNCTION to successive cars of all ARGS.
  905.           Return the list of results."
  906.             ;; If no list is exhausted,
  907.             (if (not (memq 'nil args))
  908.                 ;; apply function to CARs.
  909.                 (cons (apply f (mapcar 'car args))
  910.                       (apply 'mapcar* f
  911.                              ;; Recurse for rest of elements.
  912.                              (mapcar 'cdr args)))))
  913.  
  914.           (mapcar* 'cons '(a b c) '(1 2 3 4))
  915.                => ((a . 1) (b . 2) (c . 3))
  916.  
  917.  - Function: mapconcat FUNCTION SEQUENCE SEPARATOR
  918.      `mapconcat' applies FUNCTION to each element of SEQUENCE: the
  919.      results, which must be strings, are concatenated.  Between each
  920.      pair of result strings, `mapconcat' inserts the string SEPARATOR.
  921.      Usually SEPARATOR contains a space or comma or other suitable
  922.      punctuation.
  923.  
  924.      The argument FUNCTION must be a function that can take one
  925.      argument and return a string.
  926.  
  927.           (mapconcat 'symbol-name
  928.                      '(The cat in the hat)
  929.                      " ")
  930.                => "The cat in the hat"
  931.  
  932.           (mapconcat (function (lambda (x) (format "%c" (1+ x))))
  933.                      "HAL-8000"
  934.                      "")
  935.                => "IBM.9111"
  936.  
  937. 
  938. File: lispref.info,  Node: Anonymous Functions,  Next: Function Cells,  Prev: Mapping Functions,  Up: Functions
  939.  
  940. Anonymous Functions
  941. ===================
  942.  
  943.    In Lisp, a function is a list that starts with `lambda', a byte-code
  944. function compiled from such a list, or alternatively a primitive
  945. subr-object; names are "extra".  Although usually functions are defined
  946. with `defun' and given names at the same time, it is occasionally more
  947. concise to use an explicit lambda expression--an anonymous function.
  948. Such a list is valid wherever a function name is.
  949.  
  950.    Any method of creating such a list makes a valid function.  Even
  951. this:
  952.  
  953.      (setq silly (append '(lambda (x)) (list (list '+ (* 3 4) 'x))))
  954.      => (lambda (x) (+ 12 x))
  955.  
  956. This computes a list that looks like `(lambda (x) (+ 12 x))' and makes
  957. it the value (*not* the function definition!) of `silly'.
  958.  
  959.    Here is how we might call this function:
  960.  
  961.      (funcall silly 1)
  962.      => 13
  963.  
  964. (It does *not* work to write `(silly 1)', because this function is not
  965. the *function definition* of `silly'.  We have not given `silly' any
  966. function definition, just a value as a variable.)
  967.  
  968.    Most of the time, anonymous functions are constants that appear in
  969. your program.  For example, you might want to pass one as an argument
  970. to the function `mapcar', which applies any given function to each
  971. element of a list.  Here we pass an anonymous function that multiplies
  972. a number by two:
  973.  
  974.      (defun double-each (list)
  975.        (mapcar '(lambda (x) (* 2 x)) list))
  976.      => double-each
  977.      (double-each '(2 11))
  978.      => (4 22)
  979.  
  980. In such cases, we usually use the special form `function' instead of
  981. simple quotation to quote the anonymous function.
  982.  
  983.  - Special Form: function FUNCTION-OBJECT
  984.      This special form returns FUNCTION-OBJECT without evaluating it.
  985.      In this, it is equivalent to `quote'.  However, it serves as a
  986.      note to the Emacs Lisp compiler that FUNCTION-OBJECT is intended
  987.      to be used only as a function, and therefore can safely be
  988.      compiled.  Contrast this with `quote', in *Note Quoting::.
  989.  
  990.    Using `function' instead of `quote' makes a difference inside a
  991. function or macro that you are going to compile.  For example:
  992.  
  993.      (defun double-each (list)
  994.        (mapcar (function (lambda (x) (* 2 x))) list))
  995.      => double-each
  996.      (double-each '(2 11))
  997.      => (4 22)
  998.  
  999. If this definition of `double-each' is compiled, the anonymous function
  1000. is compiled as well.  By contrast, in the previous definition where
  1001. ordinary `quote' is used, the argument passed to `mapcar' is the
  1002. precise list shown:
  1003.  
  1004.      (lambda (x) (* x 2))
  1005.  
  1006. The Lisp compiler cannot assume this list is a function, even though it
  1007. looks like one, since it does not know what `mapcar' does with the
  1008. list.  Perhaps `mapcar' will check that the CAR of the third element is
  1009. the symbol `*'!  The advantage of `function' is that it tells the
  1010. compiler to go ahead and compile the constant function.
  1011.  
  1012.    We sometimes write `function' instead of `quote' when quoting the
  1013. name of a function, but this usage is just a sort of comment.
  1014.  
  1015.      (function SYMBOL) == (quote SYMBOL) == 'SYMBOL
  1016.  
  1017.    See `documentation' in *Note Accessing Documentation::, for a
  1018. realistic example using `function' and an anonymous function.
  1019.  
  1020. 
  1021. File: lispref.info,  Node: Function Cells,  Next: Inline Functions,  Prev: Anonymous Functions,  Up: Functions
  1022.  
  1023. Accessing Function Cell Contents
  1024. ================================
  1025.  
  1026.    The "function definition" of a symbol is the object stored in the
  1027. function cell of the symbol.  The functions described here access, test,
  1028. and set the function cell of symbols.
  1029.  
  1030.    See also the function `indirect-function' in *Note Function
  1031. Indirection::.
  1032.  
  1033.  - Function: symbol-function SYMBOL
  1034.      This returns the object in the function cell of SYMBOL.  If the
  1035.      symbol's function cell is void, a `void-function' error is
  1036.      signaled.
  1037.  
  1038.      This function does not check that the returned object is a
  1039.      legitimate function.
  1040.  
  1041.           (defun bar (n) (+ n 2))
  1042.                => bar
  1043.           (symbol-function 'bar)
  1044.                => (lambda (n) (+ n 2))
  1045.           (fset 'baz 'bar)
  1046.                => bar
  1047.           (symbol-function 'baz)
  1048.                => bar
  1049.  
  1050.    If you have never given a symbol any function definition, we say that
  1051. that symbol's function cell is "void".  In other words, the function
  1052. cell does not have any Lisp object in it.  If you try to call such a
  1053. symbol as a function, it signals a `void-function' error.
  1054.  
  1055.    Note that void is not the same as `nil' or the symbol `void'.  The
  1056. symbols `nil' and `void' are Lisp objects, and can be stored into a
  1057. function cell just as any other object can be (and they can be valid
  1058. functions if you define them in turn with `defun').  A void function
  1059. cell contains no object whatsoever.
  1060.  
  1061.    You can test the voidness of a symbol's function definition with
  1062. `fboundp'.  After you have given a symbol a function definition, you
  1063. can make it void once more using `fmakunbound'.
  1064.  
  1065.  - Function: fboundp SYMBOL
  1066.      This function returns `t' if the symbol has an object in its
  1067.      function cell, `nil' otherwise.  It does not check that the object
  1068.      is a legitimate function.
  1069.  
  1070.  - Function: fmakunbound SYMBOL
  1071.      This function makes SYMBOL's function cell void, so that a
  1072.      subsequent attempt to access this cell will cause a `void-function'
  1073.      error.  (See also `makunbound', in *Note Local Variables::.)
  1074.  
  1075.           (defun foo (x) x)
  1076.                => x
  1077.           (foo 1)
  1078.                =>1
  1079.           (fmakunbound 'foo)
  1080.                => x
  1081.           (foo 1)
  1082.           error--> Symbol's function definition is void: foo
  1083.  
  1084.  - Function: fset SYMBOL OBJECT
  1085.      This function stores OBJECT in the function cell of SYMBOL.  The
  1086.      result is OBJECT.  Normally OBJECT should be a function or the
  1087.      name of a function, but this is not checked.
  1088.  
  1089.      There are three normal uses of this function:
  1090.  
  1091.         * Copying one symbol's function definition to another.  (In
  1092.           other words, making an alternate name for a function.)
  1093.  
  1094.         * Giving a symbol a function definition that is not a list and
  1095.           therefore cannot be made with `defun'.  For example, you can
  1096.           use `fset' to give a symbol `s1' a function definition which
  1097.           is another symbol `s2'; then `s1' serves as an alias for
  1098.           whatever definition `s2' presently has.
  1099.  
  1100.         * In constructs for defining or altering functions.  If `defun'
  1101.           were not a primitive, it could be written in Lisp (as a
  1102.           macro) using `fset'.
  1103.  
  1104.      Here are examples of the first two uses:
  1105.  
  1106.           ;; Give `first' the same definition `car' has.
  1107.           (fset 'first (symbol-function 'car))
  1108.                => #<subr car>
  1109.           (first '(1 2 3))
  1110.                => 1
  1111.           
  1112.           ;; Make the symbol `car' the function definition of `xfirst'.
  1113.           (fset 'xfirst 'car)
  1114.                => car
  1115.           (xfirst '(1 2 3))
  1116.                => 1
  1117.           (symbol-function 'xfirst)
  1118.                => car
  1119.           (symbol-function (symbol-function 'xfirst))
  1120.                => #<subr car>
  1121.           
  1122.           ;; Define a named keyboard macro.
  1123.           (fset 'kill-two-lines "\^u2\^k")
  1124.                => "\^u2\^k"
  1125.  
  1126.      See also the related function `defalias', in *Note Defining
  1127.      Functions::.
  1128.  
  1129.    When writing a function that extends a previously defined function,
  1130. the following idiom is sometimes used:
  1131.  
  1132.      (fset 'old-foo (symbol-function 'foo))
  1133.      (defun foo ()
  1134.        "Just like old-foo, except more so."
  1135.        (old-foo)
  1136.        (more-so))
  1137.  
  1138. This does not work properly if `foo' has been defined to autoload.  In
  1139. such a case, when `foo' calls `old-foo', Lisp attempts to define
  1140. `old-foo' by loading a file.  Since this presumably defines `foo'
  1141. rather than `old-foo', it does not produce the proper results.  The
  1142. only way to avoid this problem is to make sure the file is loaded
  1143. before moving aside the old definition of `foo'.
  1144.  
  1145.    But it is unmodular and unclean, in any case, for a Lisp file to
  1146. redefine a function defined elsewhere.
  1147.  
  1148. 
  1149. File: lispref.info,  Node: Inline Functions,  Next: Related Topics,  Prev: Function Cells,  Up: Functions
  1150.  
  1151. Inline Functions
  1152. ================
  1153.  
  1154.    You can define an "inline function" by using `defsubst' instead of
  1155. `defun'.  An inline function works just like an ordinary function
  1156. except for one thing: when you compile a call to the function, the
  1157. function's definition is open-coded into the caller.
  1158.  
  1159.    Making a function inline makes explicit calls run faster.  But it
  1160. also has disadvantages.  For one thing, it reduces flexibility; if you
  1161. change the definition of the function, calls already inlined still use
  1162. the old definition until you recompile them.  Since the flexibility of
  1163. redefining functions is an important feature of XEmacs, you should not
  1164. make a function inline unless its speed is really crucial.
  1165.  
  1166.    Another disadvantage is that making a large function inline can
  1167. increase the size of compiled code both in files and in memory.  Since
  1168. the speed advantage of inline functions is greatest for small
  1169. functions, you generally should not make large functions inline.
  1170.  
  1171.    It's possible to define a macro to expand into the same code that an
  1172. inline function would execute.  But the macro would have a limitation:
  1173. you can use it only explicitly--a macro cannot be called with `apply',
  1174. `mapcar' and so on.  Also, it takes some work to convert an ordinary
  1175. function into a macro.  (*Note Macros::.)  To convert it into an inline
  1176. function is very easy; simply replace `defun' with `defsubst'.  Since
  1177. each argument of an inline function is evaluated exactly once, you
  1178. needn't worry about how many times the body uses the arguments, as you
  1179. do for macros.  (*Note Argument Evaluation::.)
  1180.  
  1181.    Inline functions can be used and open-coded later on in the same
  1182. file, following the definition, just like macros.
  1183.  
  1184. 
  1185. File: lispref.info,  Node: Related Topics,  Prev: Inline Functions,  Up: Functions
  1186.  
  1187. Other Topics Related to Functions
  1188. =================================
  1189.  
  1190.    Here is a table of several functions that do things related to
  1191. function calling and function definitions.  They are documented
  1192. elsewhere, but we provide cross references here.
  1193.  
  1194. `apply'
  1195.      See *Note Calling Functions::.
  1196.  
  1197. `autoload'
  1198.      See *Note Autoload::.
  1199.  
  1200. `call-interactively'
  1201.      See *Note Interactive Call::.
  1202.  
  1203. `commandp'
  1204.      See *Note Interactive Call::.
  1205.  
  1206. `documentation'
  1207.      See *Note Accessing Documentation::.
  1208.  
  1209. `eval'
  1210.      See *Note Eval::.
  1211.  
  1212. `funcall'
  1213.      See *Note Calling Functions::.
  1214.  
  1215. `ignore'
  1216.      See *Note Calling Functions::.
  1217.  
  1218. `indirect-function'
  1219.      See *Note Function Indirection::.
  1220.  
  1221. `interactive'
  1222.      See *Note Using Interactive::.
  1223.  
  1224. `interactive-p'
  1225.      See *Note Interactive Call::.
  1226.  
  1227. `mapatoms'
  1228.      See *Note Creating Symbols::.
  1229.  
  1230. `mapcar'
  1231.      See *Note Mapping Functions::.
  1232.  
  1233. `mapconcat'
  1234.      See *Note Mapping Functions::.
  1235.  
  1236. `undefined'
  1237.      See *Note Key Lookup::.
  1238.  
  1239. 
  1240. File: lispref.info,  Node: Macros,  Next: Loading,  Prev: Functions,  Up: Top
  1241.  
  1242. Macros
  1243. ******
  1244.  
  1245.    "Macros" enable you to define new control constructs and other
  1246. language features.  A macro is defined much like a function, but instead
  1247. of telling how to compute a value, it tells how to compute another Lisp
  1248. expression which will in turn compute the value.  We call this
  1249. expression the "expansion" of the macro.
  1250.  
  1251.    Macros can do this because they operate on the unevaluated
  1252. expressions for the arguments, not on the argument values as functions
  1253. do.  They can therefore construct an expansion containing these
  1254. argument expressions or parts of them.
  1255.  
  1256.    If you are using a macro to do something an ordinary function could
  1257. do, just for the sake of speed, consider using an inline function
  1258. instead.  *Note Inline Functions::.
  1259.  
  1260. * Menu:
  1261.  
  1262. * Simple Macro::            A basic example.
  1263. * Expansion::               How, when and why macros are expanded.
  1264. * Compiling Macros::        How macros are expanded by the compiler.
  1265. * Defining Macros::         How to write a macro definition.
  1266. * Backquote::               Easier construction of list structure.
  1267. * Problems with Macros::    Don't evaluate the macro arguments too many times.
  1268.                               Don't hide the user's variables.
  1269.  
  1270.